#!/bin/bash


shellout(){
 echo 
 echo "ERROR $? in $1 commande";
 echo
 #rm -rf "$FAKE_ROOT"
 #"$STARTDIR/kakola.app" "$PKGNAME"
 exit 2;
}

run_cmd(){
    echo  "  $1" && $1 > /dev/null 2>&1 || shellout "$1" ; echo
}

umask 022
VERSION=`tail -1 ../src/VERSION`
CURRENTUSER=`logname`
WHOAMI=`whoami`

STARTDIR=`pwd`
ARCH=universal
BINARY=$STARTDIR/../binary/
mkdir -p "$STARTDIR/../build/"
PKGNAME=$STARTDIR/../build/kakola-$ARCH-$VERSION.pkg
DMGNAME=$STARTDIR/../build/kakola-$ARCH-$VERSION.dmg
PACKAGEMAKER="/Developer/usr/bin/packagemaker"
PKG_OSX_TEMPLATE=$STARTDIR/../package-macosx-kakola-templates/
FAKE_ROOT="/var/tmp/package-macosx-kakola-$VERSION/root/"
APPLICATIONS="$FAKE_ROOT/Applications/"
PROFILES="$FAKE_ROOT/Applications/kakola.app/Contents/Resources/profiles/"


if [ ! "$WHOAMI" = "root" ]
then
    echo "You are running this program with user $WHOAMI"
    echo "You must be root to create the package"
    echo "Try with sudo $0"
    echo
    exit 1
fi


if echo "$STARTDIR" | grep " "
then
    shellout "$STARTDIR contain space, dangerous existing, rename your folder"
fi

echo ""


cd "$STARTDIR/"
sudo -u $CURRENTUSER sed -e 's#appIcon.rc#icons/application-icon.icns#g' ../src/kakola.pro > /tmp/kakola.mac.x86_64.pro.tmp
sed -e "s/CONFIG += x86_64/CONFIG += x86 x86_64/g"  /tmp/kakola.mac.x86_64.pro.tmp >  /tmp/kakola.mac.universal.pro
cp /tmp/kakola.mac.universal.pro ../src/kakola.mac.pro
rm /tmp/kakola.mac.x86_64.pro.tmp
rm -rf "$FAKE_ROOT" "$PKGNAME" > /dev/null 2>&1

echo
cd "$STARTDIR"
echo "Building kakola project ..."
CMD="sudo -u $CURRENTUSER sh ../src/build.mac.sh"
[ "$1" = "NO_COMPILE" ] || run_cmd "$CMD"

echo
cd "$STARTDIR/../src/"
echo "Building macdeployqt project ..."
CMD="chown -R $CURRENTUSER:admin macdeployqt"
run_cmd "$CMD"
CMD="cd  macdeployqt/macdeployqt"
run_cmd "$CMD"
CMD="sudo -u $CURRENTUSER qmake -spec macx-g++ -r"
run_cmd "$CMD"
CMD="sudo -u $CURRENTUSER make"
run_cmd "$CMD"
MACDEPLOYQT="macdeployqt/bin/macdeployqt"


echo
cd "$STARTDIR/../src/"
echo "Running $MACDEPLOYQT"
CMD="sudo -u $CURRENTUSER $MACDEPLOYQT kakola.app -dmg"
run_cmd "$CMD"
rm -f kakola.dmg



echo
echo -n "Creating directory ... $FAKE_ROOT"
rm -rf "$FAKE_ROOT"
CMD="mkdir -p $FAKE_ROOT"
run_cmd "$CMD"

echo
echo "Create directory $APPLICATIONS"
CMD="mkdir -p $APPLICATIONS"
run_cmd "$CMD"

CMD="rm -rf $APPLICATIONS/kakola.app"
run_cmd "$CMD"

CMD="cp -R kakola.app $APPLICATIONS"
run_cmd "$CMD"

CMD="cd $FAKE_ROOT"
run_cmd "$CMD"

CMD="chmod -R 755 $FAKE_ROOT" 
run_cmd "$CMD"

echo
echo "Copy documentations, ressources, translate, ... into package"
CMD="mkdir -p $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"

CMD="cp -R $STARTDIR/../documentations $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"
CMD="mkdir $APPLICATIONS/kakola.app/Contents/Resources/documentations/translate/"
run_cmd "$CMD"
CMD="cp $STARTDIR/../src/kakola_*.qm $APPLICATIONS/kakola.app/Contents/Resources/documentations/translate/"
run_cmd "$CMD"
CMD="cp $STARTDIR/../src/VERSION $APPLICATIONS/kakola.app/Contents/Resources/" 
run_cmd "$CMD"
CMD="mkdir -p ${FAKE_ROOT}/usr/bin/"
run_cmd "$CMD"

cp "$STARTDIR/../src/{*.sh\*.pl}" ${FAKE_ROOT}/usr/bin/
echo"cp $STARTDIR/../src/{*.sh\*.pl} ${FAKE_ROOT}/usr/bin/"
echo $CMD

echo
echo "Creating directory ..."
CMD="mkdir -p $PROFILES"
run_cmd "$CMD"

CMD="cp $STARTDIR/../profiles/mac/*.profile $PROFILES"
run_cmd "$CMD"
CMD="mkdir -p $APPLICATIONS/kakola.app/Contents/Resources/Binary/"
run_cmd "$CMD"
CMD="cp $STARTDIR/../src/icons/background.tiff $APPLICATIONS/kakola.app/Contents/Resources/"
run_cmd "$CMD"


echo
echo "Changing owner and permission"
CMD="chown -R root:wheel $APPLICATIONS"
run_cmd "$CMD"
CMD="chown -R root:wheel $APPLICATIONS/kakola.app"
run_cmd "$CMD"
CMD="chmod -R 775 $APPLICATIONS/kakola.app"
run_cmd "$CMD"


rm -rf $PKGNAME
echo;
echo "Creating package $PKGNAME"
CMD="$PACKAGEMAKER -build -p $PKGNAME -f $FAKE_ROOT -ds  \
 -r $PKG_OSX_TEMPLATE/resources/ \
 -s $PKG_OSX_TEMPLATE/resources/ \
 -i $PKG_OSX_TEMPLATE/Info.plist \
 -d $PKG_OSX_TEMPLATE/Description.plist"
run_cmd "$CMD"

CMD="defaults delete $PKG_OSX_TEMPLATE/Info.plist IFPkgPathMappings"
echo "  $CMD" && $CMD > /dev/null 2>&1 
CMD="cp $PKG_OSX_TEMPLATE/Info.plist $PKGNAME/Contents/Info.plist"
run_cmd "$CMD"
CMD="cp $STARTDIR/../src/icons/background.tiff $PKGNAME/Contents/Resources/"
run_cmd "$CMD"
CMD="rm -f $PKGNAME/Contents/Resources/TokenDefinitions.plist"
run_cmd "$CMD"

echo "Creating DMG file …."
rm -rf "$STARTDIR/../build/kakola-x86_64-$VERSION.dmg"
CMD="hdiutil create $DMGNAME -srcfolder $PKGNAME -ov"
run_cmd "$CMD"

echo
echo
echo "Files bellow  have been generated :"
echo
echo "Directory : $STARTDIR/../build/"
echo
cd "$STARTDIR/../build/"
du -sh "$DMGNAME"
du -sh "$PKGNAME"
echo
echo
